#include "Main.h"

char* tmpString = new char[200];

void HandleMessages(BaseMessage msg)
{
	WORD header = msg.header;

	Param_t in1 = msg.Param1;
	Param_t in2 = msg.Param2;
	Param_t in3 = msg.Param3;
	Param_t in4 = msg.Param4;
	Param_t in5 = msg.Param5;

	BaseMessage outmsg = BaseMessage();

	outmsg.header = header;

	char* instr = msg.buffer;

	CPlayer* player = NULL;
	CPlayer* playerEx = NULL;
	CParty::PartyMember* ptMember = NULL;

	__try {

	switch(header)
	{
		// COMMAND HANDLING

		/*	Temporary command
			No parameters */
	case CA_TestCommand:
		sprintf_s(tmpString, 200, "ReceivePacket: 0x%08X\nSendPacket: 0x%08X",
			Mem->GetAddress("ReceivePacket"), Mem->GetAddress("SendPacket"));
		MessageBox(NULL, tmpString, "Test Command", NULL);
		break;

		/*	Set speed of your self
			Parameter: float (1-10 best) */
	case CA_SetSpeed:
		player = CPlayerMgr::GetMe();
		player->SetSpeed(in1.f);
		break;

		/*	Moves to x,y coords (and map)
			Parameter: float & float (& map id) */
	case CA_MoveTo:
		if(in3.i != 0)
			MoveToMapXY(in1.f, in2.f, in3.i);
		else
			MoveToMapXY(in1.f, in2.f);
		break;

		/*	Uses the item at the specified index
			Parameter: index (1-based) */
	case CA_UseItem:
		CBag::UseItem(in1.i - 1);
		break;

		/*	Selects the specified player
			Parameter: id (or name) */
	case CA_SelectTarget:
		if(instr[0])
			player = CPlayerMgr::FindByName(instr);
		else
			player = CPlayerMgr::FindPlayerById(in1.i);
		CPlayerMgr::SelectTarget(player->GetUniqueId());
		break;

		/*	Use skill on current target
			Parameter: slot (1 to 26) */
	case CA_UseSkill:
		UseSkill(in1.i - 1);
		break;

		/*	Loots an entire item pack if possible
			No parameters */
	case CA_LootItem:
		LootItem();
		break;

		/*	Starts summoning the pet specified by index
			Parameter: index (1-based) */
	case CA_SummonPet:
		CPet::SummonPet(in1.i - 1);
		break;

		/*	Bring up the npc dialog with specified npc
			Parameter: player id */
	case CA_TalkToNpc:
		player = CPlayerMgr::GetMe();
		player->TryTalkWithNpc(in1.i);
		break;

		/*	Take on quest with specified id, requires open npc dialog
			Parameter: quest id (see CA_GetSelectedQuestId) */
	case CA_QuestAccept:
		CNpcDialog::AcceptQuest(in1.i);
		break;

		/*	Get quest reward for the specified id, requires open npc dialog
			Parameter: quest id (see CA_GetSelectedQuestId) */
	case CA_QuestReward:
		CNpcDialog::RewardQuest(in1.i);
		break;

		/*	Destroy item specified by index
			Parameter: index (1-based) */
	case CA_DestroyItem:
		DestroyItem(in1.i - 1);
		break;

		/*	Repair all items
			No parameters */
	case CA_RepairItems:
		RepairItems();
		break;

		/*	Change realm instantly
			Parameter: realm id */
	case CA_ChangeRealm:
		CServer::ChangeRealm(in1.i);
		break;

		/*	Use item from bag by its item id
			Parameter: item id */
	case CA_UseItemByItemId:
		CBag::UseItemByItemId(in1.i);
		break;

		/*	Start following specified player
			Parameter: player id (or name) */
	case CA_FollowPlayer:
		if(instr[0])
			player = CPlayerMgr::FindByName(instr);
		else
			player = CPlayerMgr::FindPlayerById(in1.i);
		FollowPlayer(player->GetUniqueId());
		break;

		/*	Interact/attack specified player/object
			Parameter: player id (or name) */
	case CA_Interact:
		if(instr[0])
			player = CPlayerMgr::FindByName(instr);
		else
			player = CPlayerMgr::FindPlayerById(in1.i);
		CPlayerMgr::Interact(player->GetUniqueId());
		break;

		/*	Enable or disable auto-looting
			Parameter: boolean */
	case CA_AutoLootOnOff:
		AutoLootOnOff(in1.i ? true : false);
		break;

		//REQUEST HANDLING

		/*	Temporary request
			Parameter: integer
			Parameter: integer */
	case CA_TestRequest:
		out1.i = in1.i;
		out2.i = in2.i;
		out3.i = in3.i;
		out4.i = in4.i;
		out5.i = in5.i;
		strcpy_s(outstr, 100, tmpString);
		pipe->SetResponse(outmsg);
		break;

		/*	Get the id of the currently selected player
			No parameters */
	case CA_GetCurrentTargetId:
		out1.i = CTarget::GetCurrentTargetId();
		pipe->SetResponse(outmsg);
		break;

		/*	Start player iteration and get pointer to first player
			No parameters */
	case CA_PlayerIterFirst:
		out1.d = (dword)CPlayerMgr::IterGetFirst();
		player = CPlayerMgr::GetPlayerFromIter((byte*)out1.d);
		out2.i = player->GetUniqueId();
		pipe->SetResponse(outmsg);
		break;

		/*	Get the next player in the iteration
			Parameter: ptr to playerlist [0] */
	case CA_PlayerIterNext:
		out1.d = (dword)CPlayerMgr::IterGetNext((byte*)in1.d);
		player = CPlayerMgr::GetPlayerFromIter((byte*)out1.d);
		out2.i = player->GetUniqueId();
		pipe->SetResponse(outmsg);
		break;

		/*	Get the item id of a specified item in bag
			Parameter: index (1-based) */
	case CA_GetItemIdByIndex:
		out1.i = CBag::GetItemIdByIndex(in1.i - 1);
		pipe->SetResponse(outmsg);
		break;

		/*	Get the x,y coordinates (and map id) of the specified player
			Parameter: player id (or name) */
	case CA_GetCoords:
		if(instr[0])
			player = CPlayerMgr::FindByName(instr);
		else
			player = CPlayerMgr::FindPlayerById(in1.i);
		out1.f = player->GetX();
		out2.f = player->GetY();
		out3.i = GetMapId();
		pipe->SetResponse(outmsg);
		break;

		/*	Get distance from player to player
			Parameter: player id & player id */
	case CA_GetDistFromPlayerToPlayer:
		player = CPlayerMgr::FindPlayerById(in1.i);
		playerEx = CPlayerMgr::FindPlayerById(in2.i);
		out1.f = player->GetDistanceToPlayer(playerEx);
		pipe->SetResponse(outmsg);
		break;

		/*	Get the type of specified player
			Parameter: player id (or name) */
	case CA_GetPlayerType:
		if(instr[0])
			player = CPlayerMgr::FindByName(instr);
		else
			player = CPlayerMgr::FindPlayerById(in1.i);
		out1.i = player->GetPlayerType();
		pipe->SetResponse(outmsg);
		break;

		/*	Get the name of player
			Parameter: player id */
	case CA_GetPlayerName:
		player = CPlayerMgr::FindPlayerById(in1.i);
		strcpy_s(outstr, 100, player->GetName());
		pipe->SetResponse(outmsg);
		break;

		/*	Get the player id of your character
			No parameters */
	case CA_GetMyId:
		player = CPlayerMgr::GetMe();
		out1.i = player->GetUniqueId();
		pipe->SetResponse(outmsg);
		break;

		/*	Get the health (and mana and rage) of the player
			Parameter: player id (or name) */
	case CA_GetHp:
		if(instr[0])
			player = CPlayerMgr::FindByName(instr);
		else
			player = CPlayerMgr::FindPlayerById(in1.i);
		out1.i = player->GetCurrentHp();
		out2.i = player->GetMaxHp();
		out3.i = player->GetCurrentMp();
		out4.i = player->GetMaxMp();
		out5.i = player->GetCurrentRage();
		pipe->SetResponse(outmsg);
		break;

		/*	Get the player id of your active pet
			No parameters */
	case CA_GetPetPlayerId:
		out1.i = CPet::GetActivePetPlayerId();
		pipe->SetResponse(outmsg);
		break;

		/*	Get the quest id of the one currently selected in NPC dialog
			No parameters */
	case CA_GetSelectedQuestId:
		out1.i = CNpcDialog::GetSelectedQuestId();
		pipe->SetResponse(outmsg);
		break;

		/*	Get the health (and mana) of your active pet
			No parameters */
	case CA_GetPetHp:
		out1.i = CPet::GetCurrentHp();
		out2.i = CPet::GetMaxHp();
		out3.i = CPet::GetCurrentMp();
		out4.i = CPet::GetMaxMp();
		pipe->SetResponse(outmsg);
		break;

		/*	Get the player id of player with specified name
			No parameters */
	case CA_GetPlayerIdByName:
		player = CPlayerMgr::FindByName(instr);
		out1.i = player->GetUniqueId();
		pipe->SetResponse(outmsg);
		break;

		/*	Get the server name and current realm id
			No parameters */
	case CA_GetServerRealmInfo:
		out1.i = CServer::GetCurrentRealm();
		strcpy_s(outstr, 100, CServer::GetServerName());
		pipe->SetResponse(outmsg);
		break;

		/*	Get the number of players in your party
			No parameters */
	case CA_GetPartySize:
		out1.i = CParty::GetPartySize();
		pipe->SetResponse(outmsg);
		break;

		/*	Get basic info on party member
			Parameter: index (0-5 where 0 is yourself) */
	case CA_GetPartyMemberInfo:
		ptMember = CParty::GetPartyMember(in1.i);
		out1.i = ptMember->CurrentHP;
		out2.i = ptMember->MaxHP;
		out3.i = ptMember->GroupRank;
		out4.i = ptMember->Level;
		out5.i = ptMember->RealmId;
		strcpy_s(outstr, 100, ptMember->Name);
		pipe->SetResponse(outmsg);
		break;

		/*	Get the closest player by name
			Parameter: name & bool */
	case CA_GetClosestPlayerByName:
		player = CPlayerMgr::GetClosestPlayerByName(instr, in1.i ? true : false);
		out1.i = player->GetUniqueId();
		pipe->SetResponse(outmsg);
		break;

		/*	Check if player is dead
			Parameter: name or id */
	case CA_GetIsDead:
		if(instr[0])
			player = CPlayerMgr::FindByName(instr);
		else
			player = CPlayerMgr::FindPlayerById(in1.i);
		out1.i = player->GetIsDead();
		pipe->SetResponse(outmsg);
		break;
	}

	}
	__except(1) { }

	delete player;
}